package com.suzhe.ymall.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suzhe.ymall.common.annotation.LoginAuth;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.order.client.ProductClient;
import com.suzhe.ymall.order.client.PromotionClient;
import com.suzhe.ymall.order.convert.OrderConvert;
import com.suzhe.ymall.order.enums.OrderStatusEnum;
import com.suzhe.ymall.order.param.SubmitOrderParam;
import com.suzhe.ymall.order.pojo.Order;
import com.suzhe.ymall.order.pojo.OrderStatus;
import com.suzhe.ymall.order.service.IOrderService;
import com.suzhe.ymall.order.service.IOrderStatusService;
import com.suzhe.ymall.order.vo.OrderVo;
import com.suzhe.ymall.order.vo.PayResultVo;
import com.suzhe.ymall.product.param.LockStockParam;
import com.suzhe.ymall.product.param.OrderSkuParam;
import com.suzhe.ymall.promotion.param.LockCouponParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-08-08
 */
@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Autowired
    IOrderService iOrderService;

    @Autowired
    PromotionClient promotionClient;

    @Autowired
    ProductClient productClient;

    @Autowired
    IOrderStatusService iOrderStatusService;

    @PostMapping("/submitOrder")
    @LoginAuth
    @ApiOperation(value = "提交订单")
    public ApiResEntity submitOrder(@RequestBody SubmitOrderParam submitOrderParam) {
        log.debug("submitOrder submitOrderParam:{}", submitOrderParam);
        //生成不可见订单
        Order order = iOrderService.createOrder(submitOrderParam);
        if (order == null){
            return ApiResEntity.error("创建订单失败");
        }
        ApiResEntity apiResEntity = ApiResEntity.ok();
        long actualPay = order.getTotalPay();
        //锁券
        String userCouponId = submitOrderParam.getUserCouponId();

        if (StringUtils.isNotEmpty(userCouponId)) {
            LockCouponParam lockCouponParam = new LockCouponParam();
            lockCouponParam.setOrderNo(order.getOrderNo());
            lockCouponParam.setTotalPay(actualPay);
            lockCouponParam.setUserCouponId(userCouponId);
            apiResEntity = promotionClient.lockUserCoupon(lockCouponParam);
            if (apiResEntity.isSuccess()) {
                actualPay = (Long) apiResEntity.getData();
            }
        }
        if(apiResEntity.isSuccess()){//如果锁券成功
            LockStockParam lockStockParam = new LockStockParam();
            List<OrderSkuParam> orderSkuParamList = submitOrderParam.getOrderSkuParamList();
            lockStockParam.setOrderNo(order.getOrderNo());
            lockStockParam.setOrderSkuParams(orderSkuParamList);
            //扣减库存
            apiResEntity = productClient.lockStock(lockStockParam);
        }
        if (apiResEntity.isSuccess()){ //如果锁券和扣减库存都成功
            //更新实际付款金额
            iOrderService.updateActualPay(order.getOrderNo(), actualPay);
            //更新订单状态可见
            iOrderService.updateOrderStatus(order.getOrderNo(), OrderStatusEnum.INIT);
            //发送延迟消息，如果订单在10分钟内还没有支付，则自动将订单取消
            iOrderService.sendScheduledOrderMessage(order.getOrderNo());
            return ApiResEntity.ok(order.getOrderNo());
        }else{
            //订单回滚
            iOrderService.sendOrderRollbackMessage(order.getOrderNo());
        }

        return apiResEntity;
    }

    @GetMapping("/getOrderVoByOrderNo")
    @LoginAuth
    @ApiOperation(value = "根据订单号获取订单")
    public OrderVo getOrderVoByOrderNo(String orderNo) {
        log.debug("getOrderByOrderNo orderNo:{}", orderNo);
        Order order = iOrderService.getOne(new QueryWrapper<Order>().eq("order_no", orderNo));
        return OrderConvert.MAPPER.orderToOrderVo(order);
    }

    @GetMapping("/payResultVo")
    @ApiOperation(value = "获取支付返回也需要的数据")
    public PayResultVo payResultVo(String orderNo) {
        log.debug("getOrderByOrderNo orderNo:{}", orderNo);
        Order order = iOrderService.getOne(new QueryWrapper<Order>().eq("order_no", orderNo));
        OrderStatus orderStatus = iOrderStatusService.getById(orderNo);
        PayResultVo payResultVo = new PayResultVo();
        payResultVo.setOrderNo(orderNo);
        payResultVo.setStatus(orderStatus.getStatus());
        payResultVo.setActualPay(order.getActualPay());
        payResultVo.setTotalPay(order.getTotalPay());
        return payResultVo;
    }


}
